package com.copera.cn.kaka;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import com.copera.cn.kaka.util.ActionUrls;
import com.copera.cn.kaka.util.HttpUtils;
import com.copera.cn.kaka.util.JsonUtil;
import com.copera.cn.kaka.vo.ShopVo;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class ShopPassActivity extends BaseActivity {
	
	private ApplicationCustomer appc;
	private RelativeLayout back;
	private Button loginButton;
	private EditText editText1,EditText01,EditText02;
	private Handler handler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_pass);
		appc = (ApplicationCustomer) getApplication();
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {

				case ActionUrls.FAILURE:
					Toast.makeText(ShopPassActivity.this, (String) msg.obj,
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		};
		
		initViews();
		initEvent();	
		//强制为竖屏 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	private void initViews(){
		back = (RelativeLayout) findViewById(R.id.back);
		loginButton = (Button) findViewById(R.id.loginButton);
		editText1 = (EditText) findViewById(R.id.editText1);
		EditText01 = (EditText) findViewById(R.id.EditText01);
		EditText02 = (EditText) findViewById(R.id.EditText02);
	}
	private void initEvent(){
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();

			}
		});
		loginButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if(isVilid()){
				new Thread() {
					public void run() {
						try {
							String json = HttpUtils.get(ActionUrls.URL_SHOPPASS_SAVE+"?id="+appc.getShop().getId()+"&shopPass="+editText1.getText()+"&Pass1="+EditText01.getText()+"&Pass2="+EditText02.getText(), getAppCustomer().getHttpClient());
							JSONObject jo = new JSONObject(json);
							String message = jo.getString("message");
							if(message.equals("保存成功")){
								finish();
								Message msg = handler.obtainMessage(ActionUrls.FAILURE, message);
								handler.sendMessage(msg);
							}else{
								Message msg = handler.obtainMessage(ActionUrls.FAILURE, message);
								handler.sendMessage(msg);
							}
						} catch (ClientProtocolException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					};
				}.start();				
				}	
			}
		});		
	}
	private boolean isVilid(){
		if (editText1.getText().toString() == null || "".equals(editText1.getText().toString().trim())) {
			Toast.makeText(ShopPassActivity.this, "输入旧密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (EditText01.getText().toString() == null || "".equals(EditText01.getText().toString().trim())) {
			Toast.makeText(ShopPassActivity.this, "输入新密码", Toast.LENGTH_SHORT).show();
			return false;
		}

		if (EditText02.getText().toString() == null || "".equals(EditText02.getText().toString().trim())) {
			Toast.makeText(ShopPassActivity.this, "重复输入新密码", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
	
}
